################################################################################
#
# nxp s32 s32g pfeng features
#
################################################################################
PFENG_VERSION = $(call qstrip,$(BR2_PACKAGE_S32_PFENG_SW_VERSION))
PFENG_SITE = package/nxp/s32/pfeng/pfeng-$(PFENG_VERSION)
PFENG_SITE_METHOD = local

PFENG_INSTALL_IMAGES = YES

PFENG_MDIR = $(@D)/sw/linux-pfeng

PFENG_FIRMWARE_VERSION = $(call qstrip,$(BR2_PACKAGE_S32_PFENG_FIRMWARE_VERSION))
PFENG_FIRMWARE_DIR = package/nxp/s32/pfeng/firmware/$(PFENG_FIRMWARE_VERSION)

PFENG_INSTALL_TARGET_DIR = \
	$(TARGET_DIR)/lib/modules/$(LINUX_VERSION_PROBED)/kernel/drivers/net/ethernet/nxp/pfe
PFENG_FIRMWARE_INSTALL_TARGET_DIR = \
	$(TARGET_DIR)/lib/firmware
PFENG_INSTALL_IMAGE_DIR = \
	$(BINARIES_DIR)/firmware

ifeq ($(BR2_TOOLCHAIN_BUILDROOT),y)
PLATFORM_TOOLCHAIN = $(GNU_TARGET_NAME)
else
PLATFORM_TOOLCHAIN = $(TOOLCHAIN_EXTERNAL_PREFIX)
endif

PFENG_COMMON_MAKE_OPTS += \
	PLATFORM=$(PLATFORM_TOOLCHAIN) \
	KERNELDIR=$(LINUX_DIR) \
	MDIR=$(PFENG_MDIR)

ifeq ($(BR2_PACKAGE_PFENG_USE_NVMEM),y)
PFENG_COMMON_MAKE_OPTS += PFE_CFG_USE_NVMEM=1
endif

ifeq ($(BR2_PACKAGE_PFENG_MULTI_INSTANCE),y)
PFENG_COMMON_MAKE_OPTS += PFE_CFG_MULTI_INSTANCE_SUPPORT=1
endif

ifeq ($(BR2_PACKAGE_PFENG_USE_MASTER), y)
PFENG_MASTER_MAKE_OPTS += \
	$(PFENG_COMMON_MAKE_OPTS) \
	PFE_CFG_PFE_MASTER=1
endif

ifeq ($(BR2_PACKAGE_PFENG_USE_SLAVE), y)
PFENG_SLAVE_MAKE_OPTS += \
	$(PFENG_COMMON_MAKE_OPTS) \
	PFE_CFG_PFE_MASTER=0
endif

#---------------------------------------------------------------------------------------------------------
#
# 构建命令定义
#
#---------------------------------------------------------------------------------------------------------
ifeq ($(BR2_PACKAGE_PFENG_USE_MASTER)$(BR2_PACKAGE_PFENG_USE_SLAVE),)

define PFENG_BUILD_CMDS
	$(TARGET_MAKE_ENV) $(MAKE) $(PFENG_COMMON_MAKE_OPTS) -C $(PFENG_MDIR) V=1 drv-clean
	$(TARGET_MAKE_ENV) $(MAKE) $(PFENG_COMMON_MAKE_OPTS) -C $(PFENG_MDIR) V=1 drv-build
endef
else # ifeq ($(BR2_PACKAGE_PFENG_USE_MASTER)$(BR2_PACKAGE_PFENG_USE_SLAVE),)
define PFENG_BUILD_CMDS
	$(TARGET_MAKE_ENV) $(MAKE) $(PFENG_COMMON_MAKE_OPTS) -C $(PFENG_MDIR) V=1 drv-clean
	$(if $(BR2_PACKAGE_PFENG_USE_MASTER),
		$(TARGET_MAKE_ENV) $(MAKE) $(PFENG_MASTER_MAKE_OPTS) -C $(PFENG_MDIR) V=1 drv-build
	)
	$(if $(BR2_PACKAGE_PFENG_USE_SLAVE),
		$(TARGET_MAKE_ENV) $(MAKE) $(PFENG_SLAVE_MAKE_OPTS) -C $(PFENG_MDIR) V=1 drv-build
	)
endef
endif

#---------------------------------------------------------------------------------------------------------
#
# Strip 模块
#
#---------------------------------------------------------------------------------------------------------
ifeq ($(BR2_PACKAGE_PFENG_STRIP),y)
define PFENG_STRIP_LIB_CMDS
	$(TARGET_STRIP) --strip-debug $(PFENG_MDIR)/pfeng.ko
	$(if $(BR2_PACKAGE_PFENG_USE_SLAVE),
		$(TARGET_STRIP) --strip-debug $(PFENG_MDIR)/pfeng-slave.ko
	)
endef
PFENG_POST_BUILD_HOOKS += PFENG_STRIP_LIB_CMDS
endif

#---------------------------------------------------------------------------------------------------------
#
# 安装进 target 目录
#
#---------------------------------------------------------------------------------------------------------
define PFENG_INSTALL_TARGET_CMDS
	test -d $(PFENG_INSTALL_TARGET_DIR) || mkdir -p $(PFENG_INSTALL_TARGET_DIR)
	$(INSTALL) -D -m 0755 $(PFENG_MDIR)/pfeng.ko $(PFENG_INSTALL_TARGET_DIR)/pfeng.ko
	$(if $(BR2_PACKAGE_PFENG_USE_SLAVE),
		$(INSTALL) -D -m 0755 $(PFENG_MDIR)/pfeng-slave.ko $(PFENG_INSTALL_TARGET_DIR)/pfeng-slave.ko
	)
	$(HOST_DIR)/sbin/depmod -a -b $(TARGET_DIR) $(LINUX_VERSION_PROBED)
endef

define PFENG_FIMWARE_INSTALL_TARGET_DIR
	test -d $(PFENG_FIRMWARE_INSTALL_TARGET_DIR) || mkdir -p $(PFENG_FIRMWARE_INSTALL_TARGET_DIR)
	$(INSTALL) -D -m 0755 $(PFENG_FIRMWARE_DIR)/s32g_pfe_class.fw $(PFENG_FIRMWARE_INSTALL_TARGET_DIR)/s32g_pfe_class.fw
	$(INSTALL) -D -m 0755 $(PFENG_FIRMWARE_DIR)/s32g_pfe_util.fw $(PFENG_FIRMWARE_INSTALL_TARGET_DIR)/s32g_pfe_util.fw
endef
PFENG_POST_INSTALL_TARGET_HOOKS += PFENG_FIMWARE_INSTALL_TARGET_DIR

#---------------------------------------------------------------------------------------------------------
#
# 安装到 firmware 到 images 目录
#
#---------------------------------------------------------------------------------------------------------
define PFENG_INSTALL_IMAGES_CMDS
	test -d $(PFENG_INSTALL_IMAGE_DIR) || mkdir -p $(PFENG_INSTALL_IMAGE_DIR)
	$(INSTALL) -D -m 0755 $(PFENG_FIRMWARE_DIR)/s32g_pfe_class.fw $(PFENG_INSTALL_IMAGE_DIR)/s32g_pfe_class.fw
	$(INSTALL) -D -m 0755 $(PFENG_FIRMWARE_DIR)/s32g_pfe_util.fw $(PFENG_INSTALL_IMAGE_DIR)/s32g_pfe_util.fw
endef

$(eval $(generic-package))
